<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <base href="<%=request.getContextPath()%>/"/>
    <script type="text/javascript" src="static/js/jquery-2.1.1.min.js"></script>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@include file="../layUI.jsp" %>
</head>
<body>

<form action="doCus?action=cusQueryLike&pageNumber=1" method="post">
    <label>ID</label>
    <input type="text" name="id">
    <label>姓名</label>
    <input type="text" name="cusName">
    <label>姓别</label>
    <input type="text" name="cusSex">
    <input class="layui-btn" type="button" value="查询" onclick="searchCustomer(1)">
</form>

<table width="100%" style="text-align: center" class="layui-table">
    <thead>
    <tr>
        <th>ID</th>
        <th>姓名</th>
        <th>登录账号</th>
        <th>Email</th>
        <th>性别</th>
        <th>头像</th>
        <th>爱好</th>
        <th>证件号</th>
        <th>生日</th>
        <th colspan="2">操作</th>
    </tr>
    </thead>
    <thead id="theBody">

    </thead>
    <thead>
    <tr>
        <td colspan="10">
            <%@include file="../Page.jsp" %>
        </td>
    </tr>
    </thead>
     </table>
    <script type="text/javascript">
        //初始化数据
        var currentPage = 1;  //当前页码
        var totalPags;
        //过滤查询
        function searchCustomer(currentPage) {
            var id = $("[name=id]").val();
            var cusName = $("[name=cusName]").val();
            var cusSex = $("[name=cusSex]").val();
            $.getJSON("/doCus", {
                "action": "cusQueryLike",
                "currentPage": currentPage,
                "id": id,
                "cusName": cusName,
                "cusSex": cusSex
            }, callback);
            //回调
            function callback(data) {
                $("#theBody").html("");
                for (var i = 0; i < data.list.length; i++) {
                    $("#theBody").append("<tr>" +
                        "<td>" + data.list[i].id + "</td>" +
                        "<td>" + data.list[i].cusName + "</td>" +
                        "<td>" + data.list[i].cusLoginName + "</td>" +
                        "<td>" + data.list[i].cusEmail + "</td>" +
                        "<td>" + data.list[i].cusSex + "</td>" +
                        "<td><img src=\"/CusImage/" + data.list[i].cusPhoto + "\"/></td>" +
                        "<td>" + data.list[i].cusHobby + "</td>" +
                        "<td>" + data.list[i].cusCode + "</td>" +
                        "<td>" + data.list[i].cusBirthday + "</td>" +
                        "<td>" +
                        "<a href='/page/customer/cusUpdate.jsp?id="+data.list[i].id+"'>编辑</a>" +
                        "&nbsp; &nbsp;&nbsp;" +
                        "<a href='javascript:void(0)' onclick='delScus("+data.list[i].id+",this)'>删除</a>" +
                        "</td>" +
                        "</tr>")
                }

                $("#total").html(data.totalCount);
                $("#totalPages").html(data.totalPages);
                $("#pageNo").html(currentPage);
                $("#jump1").html("");
                for (var i = 1; i <= parseInt(data.totalPages); i++){
                    $("#jump1").append("<option name=\"op1\" value=\"" + i + "\">" + i + "页</option>")
                }
                totalPags =  $("#totalPages").html();
                document.getElementsByName("op1")[currentPage-1].selected=true;
            }
        }

        //初始化加载数据
        $(function () {
            searchCustomer(currentPage);
            //首页
            $("#begin").click(function () {
                currentPage = 1;
                SelSmall(currentPage);
                $("#pageNo").html(currentPage);
            })
            //上一页
            $("#prev").click(function () {
                currentPage = parseInt(currentPage )-1;
                if (parseInt(currentPage) <1) {
                    alert("已经是第一页了")
                } else {
                    searchCustomer(currentPage);
                    $("#pageNo").html(currentPage);
                }
            })
            //下一页
            $("#next").click(function () {
                currentPage = parseInt(currentPage)+1;
                if (parseInt(currentPage) > parseInt($("#totalPages").html())) {
                    alert("已经最后一页了");
                    return;
                } else {
                    searchCustomer(currentPage);
                    $("#pageNo").html(currentPage);
                }
            })
            //最后一页
            $("#end").click(function () {
                currentPage = parseInt($("#totalPages").html());
                searchCustomer(currentPage);
                $("#pageNo").html(currentPage);
            })
            //到第几页
            $("#jump").click(function () {
                currentPage=document.getElementById("jump1").value;
                /*   if (currentPage > $("#totalPages").html() || currentPage < 1) {
                       alert("填写有误!");
                       $("#inputPage").val("");
                       return;
                   }*/
                searchCustomer(parseInt(currentPage));
                $("#pageNo").html(currentPage);
            })
        })

        //删除
        function delScus(id,btn) {
            $.getJSON("/doCus",{"action":"delSercus","id":id},callback);
            function callback(data) {
                if(data.flag=="true"){
                    var option = confirm("确认删除吗？");
                    if (option) {
                        $(btn).parent().parent().remove();
                        alert("删除成功！");
                    } else {
                        alert("您取消了删除！")
                    }
                }else{
                    alert("删除失败！")
                }
            }
        }
    </script>

</body>
</html>
